package elicecn.netpay;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import CCBSign.RSASig;
import elicecn.util.StringUtils;

/**
 * 建设银行网银返回信息验证
 * 
 * @author 屹砾
 * @email eli.wuhan@gmail.com
 * @time 2009-8-7
 */
public class CCBReceive implements Serializable {
	private static final long serialVersionUID = 1520948351394509450L;
	protected static final Log log = LogFactory.getLog(CCBReceive.class);
	private static final CCBSign.RSASig rsaSign = new RSASig();

	private String posId;
	private String branchId;
	private String orderId;
	private String payment;
	private String curCode;
	private String remark1;
	private String remark2;
	private String success;
	private String sign;
	private String publicKey;

	/**
	 * CCB网银构造方法
	 * 
	 * @param posId
	 *            商户柜台代码 CHAR(9)
	 * @param branchId
	 *            分行代码 CHAR(9)
	 * @param orderId
	 *            定单号 CHAR(30)
	 * @param payment
	 *            付款金额 NUMBER(16,2)
	 * @param curCode
	 *            币种 CHAR(2)
	 * @param remark1
	 *            备注一 CHAR(30)
	 * @param remark2
	 *            备注二 CHAR(30)
	 * @param success
	 *            成功标志 CHAR(1)
	 * @param sign
	 *            数字签名 CHAR(256)
	 * @param publicKey
	 *            公钥
	 */
	public CCBReceive(String posId, String branchId, String orderId,
			String payment, String curCode, String remark1, String remark2,
			String success, String sign, String publicKey) {
		this.posId = posId;
		this.branchId = branchId;
		this.orderId = orderId;
		this.payment = payment;
		this.curCode = curCode;
		this.remark1 = remark1;
		this.remark2 = remark2;
		this.success = success;
		this.sign = sign;
		this.publicKey = publicKey;
	}

	/**
	 * 校验服务端返回的参数
	 * 
	 * @return
	 */
	public boolean check() {
		StringBuffer buf = new StringBuffer();
		buf.append("POSID=").append(StringUtils.acquireNotNull(posId));
		buf.append("&BRANCHID=").append(StringUtils.acquireNotNull(branchId));
		buf.append("&ORDERID=").append(StringUtils.acquireNotNull(orderId));
		buf.append("&PAYMENT=").append(StringUtils.acquireNotNull(payment));
		buf.append("&CURCODE=").append(StringUtils.acquireNotNull(curCode));
		buf.append("&REMARK1=").append(StringUtils.acquireNotNull(remark1));
		buf.append("&REMARK2=").append(StringUtils.acquireNotNull(remark2));
		buf.append("&SUCCESS=").append(StringUtils.acquireNotNull(success));
		rsaSign.setPublicKey(publicKey);
		return rsaSign.verifySigature(sign, buf.toString());
	}
}
